package com.itttq.im.model.to;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "im", type = "msg")
public class MsgTo implements Serializable {
    private static final long serialVersionUID = 6967408089863247121L;
    @Id
    private String id;
    //    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
    /**
     * 发送方，可以为空。广播消息的时候
     */
    @Field(type = FieldType.Keyword)
    private String from;
    /**
     * 收件人，根据消息类型，此值的意义也不一样：单聊时是userId；群聊时是群组ID
     */
    @Field(type = FieldType.Keyword)
    @NotNull(message = "消息接收方不能为空")
    private String to;
    @Field(type = FieldType.Text, index = false)
    @NotNull(message = "消息体不能为空")
    private String body;
    @Field(type = FieldType.Integer)
    @NotNull(message = "消息类型不能为空")
    private Integer type;
    @Field(type = FieldType.Date, index = false)
    private Date createTime;

    public enum MsgType {
        // 点对点消息 - 单聊
        P2P(1),
        // 群组消息 - 群聊
        GROUP(2),
        // 广播消息 - 给所有人发
        BROADCAST(3),
        // 心跳消息 - 定时检测
        HEARTBEAT(4),
        // 通知消息 - 转发不保存
        NOTICE(5);
        private int code;

        MsgType(int code) {
            this.code = code;
        }

        public static MsgType valueOf(int code) {
            for (MsgType msgType : MsgType.values()) {
                if (msgType.getCode() == code) {
                    return msgType;
                }
            }
            return null;
        }

        public int getCode() {
            return this.code;
        }
    }

}
